<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;
use think\Exception;//事务处理

class Co_branch extends Model
{
    //分公司的添加编辑
    public function addeditbranch($data)
    {
        $ckdata = notEmpty(['name' => '公司名称', 'addr' => '地址', 'province_id' => '省份', 'city_id' => '城市', 'works' => '员工数量']);
        if ($ckdata['status'] == 2) {
            return $ckdata;
        }
        if (isset($data['co_id']) && $data['co_id']) {
            $infos = Db::name('company')->where('id', $data['co_id'])->value('status');
            if ($infos == 'LOCK') {
                return array('msg' => $data['name'] . lang("status_locked_can't_be_edit"), 'status' => 0);
            }
        }
        //拼接地址
        $address = model('company')->addresssget($data['country_id'], $data['province_id'], $data['city_id'], $data['town_id'], $data['lang']);
        $worksnum = model('company')->worksexdata($data['works'], 1);
        $admin = session('admin');
        $update_date = getTimeDate(null, 12);
        if ($data['biaozhi'] == 'branchadd') {
            $infocompany = $this->where('name', $data['name'])->find();
            if ($infocompany) {
                return array('msg' => $data['name'] . lang("already_exists"), 'status' => 3);
            }
            if (isset($data['isedit']) && $data['isedit'] == 1) {
                if($data['lock'] == 'true'){
                    $data['lock'] = '1';
                }elseif ($data['lock'] == 'false'){
                    $data['lock'] = '0';
                }
                $data['create_id'] = $data['update_id'] = session('userid');
                $data['create_at'] = $data['update_at'] = time();
                $co_branch = $this->strict(false)->insert($data);
                if ($co_branch) {
                    return array('msg' => $data['name'] . lang("added_successfully"), 'status' => 1);
                } else {
                    return array('msg' => $data['name'] . lang("add_failed"), 'status' => 0);
                }
            }else{
                return array(
                    'msg' => $data['name'] . lang("added_successfully"),
                    'status' => 1,
                    'update_at' => time(),
                    'update_date' => $update_date,
                    'update_id' => $admin['id'],
                    'updatename' => $admin['nickname'],
                    'address' => $address,
                    'worksnum' => $worksnum
                );
            }
        } elseif ($data['biaozhi'] == 'branchedit') {
            if (isset($data['isedit']) && $data['isedit'] == 1) {
                if($data['lock'] == 'true'){
                    $data['lock'] = '1';
                }elseif ($data['lock'] == 'false'){
                    $data['lock'] = '0';
                }
                unset($data['biaozhi']);
                unset($data['branchaddedit']);
                unset($data['isedit']);
                unset($data['lang']);
                $id = $data['id'];
                unset($data['id']);
                $data['update_id'] = session('userid');
                $data['update_at'] = time();
                $upid = $this->where('id', $id)->update($data);
                if ($upid !== false) {
                    return array('msg' => $data['name'] . lang("edited_successfully"), 'status' => 1);
                } else {
                    return array('msg' => $data['name'] . lang("edit_failed"), 'status' => 0);
                }
            }else{
                return array(
                    'msg' => $data['name'] . lang("edited_successfully"),
                    'status' => 1,
                    'update_at' => time(),
                    'update_date' => $update_date,
                    'update_id' => $admin['id'],
                    'updatename' => $admin['nickname'],
                    'address' => $address,
                    'worksnum' => $worksnum
                );
            }
        }
    }

    //分公司的删除
    public function delbranch($data)
    {
        $branchid = $this->where('id', $data['branchid'])->delete();
        if ($branchid) {
            return array('msg' => lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' => lang("failed_to_delete"), 'status' => 0);
        }
    }
}